package com.lw.ai.controller;

import com.lw.ai.dto.book.BookDTO;
import com.lw.ai.dto.book.BookViews;
import com.lw.ai.enums.DataType;
import com.lw.ai.mapstruct.BookMapper;
import com.lw.ai.model.Book;
import com.lw.ai.model.Result;
import com.lw.ai.model.ResultNew;
import com.lw.ai.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/book")
public class UserController {

    BookMapper bookMapper;
    BookService bookService;

    @Autowired    //构造函数注入
    public UserController(BookMapper bookMapper, BookService bookService) {
        this.bookMapper = bookMapper;
        this.bookService = bookService;
    }

    @GetMapping()
    public MappingJacksonValue showBook(){
        /*Map<String, BookDTO> dtoMap = map.entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        e -> {
                            Book b = e.getValue();
                            BookDTO dto = new BookDTO(b.getNumber(),b.getBookName(),b.getPrice(),b.getPublish());
                            return dto;
                        }
                ));///*/
        Map<String, Book> map = bookService.findBook();
        Map<String, BookDTO> dtoMap = bookMapper.toDtoMap(map);
        ResultNew response = ResultNew.success(dtoMap, DataType.BOOK_LIST);
        System.out.println(response);
        MappingJacksonValue wrapper = new MappingJacksonValue(response);
        wrapper.setSerializationView( BookViews.AdminView.class );
        return wrapper;
    }
    @GetMapping("/error")
    public ResultNew showError(){

        return ResultNew.failure(404,"戳错了 ...稍后再试");///
    }
    @GetMapping("/1/{id}")
    public Result<Book> showBookById( @PathVariable String id){
        System.out.println(id);
        return Result.success(bookService.findBookById(id));
    }
    @GetMapping("/2/{name}")
    public Book showBookByBookname( @PathVariable String name){
        //System.out.println(id);
        return null;
    }
    @PostMapping()
    public ResultNew addBook(@RequestBody Book book){   //增加一本书  json
        Book book1 = bookService.addBook(book);
        if(book1!=null)
            return ResultNew.failure(500,"增加失败，已存在此书");
        return ResultNew.success("添加成功",DataType.STRING);
    }
    //  修改一本
    @PutMapping()
    public ResultNew updateBook(@RequestBody Book book) {   //增加一本书  json
        Book book1 = bookService.updateBook(book);
        if(book1!=null)
            return ResultNew.success("修改成功",DataType.STRING);
        else
            return ResultNew.failure(500,"修改失败，没有此书");

    }
    //删除一本书
    @DeleteMapping("/{id}")
    public ResultNew deleteBook(@PathVariable String id){
        Book book = bookService.deleteBook(id);
        if(book!=null)
            return ResultNew.success("删除成功",DataType.STRING);
        else
            return ResultNew.failure(500,"删除失败，没有此书");
    }
}
